// ------------------------------------------------------------------------------
// 
// Copyright (c) 2006-2008 Swampware, Inc.
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// 
// ------------------------------------------------------------------------------
using System;

namespace Water
{
	/// <summary>
	/// Summary description for InstructionSet.
	/// </summary>
	public class InstructionSet
	{

		public static bool And(bool a, bool b)
		{
			if (!a)
			{
				return false;
			}
			return (a && b);
		}

		public static bool Or(bool a, bool b)
		{
			if (a)
			{
				return true;
			}
			return (a || b);
		}

		public static bool Not(bool a)
		{
			return !a;
		}

		public static bool IsNull(object a)
		{
			return (a == null);
		}

		public static new bool Equals(object a, object b)
		{
			if (a == null)
			{
				return false;
			}
			if (b == null)
			{
				return false;
			}
			return a.Equals(b);
		}

		public static bool Error(string message)
		{
			throw new ErrorException(message);
		}

		#region Add

		public static int Add(int a, int b)
		{
			return a + b;
		}

		public static long Add(long a, long b)
		{
			return a + b;
		}

		public static decimal Add(decimal a, decimal b)
		{
			return a + b;
		}

		public static float Add(float a, float b)
		{
			return a + b;
		}

		public static double Add(double a, double b)
		{
			return a + b;
		}

		public static double Add(object a, object b)
		{
			return System.Convert.ToDouble(a) + System.Convert.ToDouble(b);
		}

		#endregion

		#region Subtract

		public static int Subtract(int a, int b)
		{
			return a - b;
		}

		public static long Subtract(long a, long b)
		{
			return a - b;
		}

		public static decimal Subtract(decimal a, decimal b)
		{
			return a - b;
		}

		public static float Subtract(float a, float b)
		{
			return a - b;
		}

		public static double Subtract(double a, double b)
		{
			return a - b;
		}

		public static double Subtract(object a, object b)
		{
			return System.Convert.ToDouble(a) - System.Convert.ToDouble(b);
		}

		#endregion

		#region Multiply

		public static int Multiply(int a, int b)
		{
			return a * b;
		}

		public static long Multiply(long a, long b)
		{
			return a * b;
		}

		public static decimal Multiply(decimal a, decimal b)
		{
			return a * b;
		}

		public static float Multiply(float a, float b)
		{
			return a * b;
		}

		public static double Multiply(double a, double b)
		{
			return a * b;
		}

		public static double Multiply(object a, object b)
		{
			return System.Convert.ToDouble(a) * System.Convert.ToDouble(b);
		}

		#endregion

		#region Divide

		public static int Divide(int a, int b)
		{
			return a / b;
		}

		public static long Divide(long a, long b)
		{
			return a / b;
		}

		public static int Remainder(int a, int b)
		{
			return a % b;
		}

		public static long Remainder(long a, long b)
		{
			return a % b;
		}

		public static decimal Divide(decimal a, decimal b)
		{
			return a / b;
		}

		public static float Divide(float a, float b)
		{
			return a / b;
		}

		public static double Divide(double a, double b)
		{
			return a / b;
		}

		public static double Divide(object a, object b)
		{
			return System.Convert.ToDouble(a) / System.Convert.ToDouble(b);
		}

		#endregion

		#region Square/SquareRoot

		public static long Square(int a)
		{
			return a * a;
		}

		public static long Square(long a)
		{
			return a * a;
		}

		public static decimal Square(decimal a)
		{
			return a * a;
		}

		public static double Square(float a)
		{
			return a * a;
		}

		public static double Square(double a)
		{
			return a * a;
		}

		public static double SquareRoot(int a)
		{
			return System.Math.Sqrt(a);
		}

		public static double SquareRoot(long a)
		{
			return System.Math.Sqrt(a);
		}

		public static double SquareRoot(decimal a)
		{
			return System.Math.Sqrt(System.Convert.ToDouble(a));
		}

		public static double SquareRoot(float a)
		{
			return System.Math.Sqrt(a);
		}

		public static double SquareRoot(double a)
		{
			return System.Math.Sqrt(a);
		}

		#endregion

		#region GreaterThan

		public static bool GreaterThan(int a, int b)
		{
			return (a > b);
		}

		public static bool GreaterThan(long a, long b)
		{
			return (a > b);
		}

		public static bool GreaterThan(decimal a, decimal b)
		{
			return (a > b);
		}

		public static bool GreaterThan(float a, float b)
		{
			return (a > b);
		}

		public static bool GreaterThan(double a, double b)
		{
			return (a > b);
		}

		#endregion

		#region LessThan

		public static bool LessThan(int a, int b)
		{
			return (a < b);
		}

		public static bool LessThan(long a, long b)
		{
			return (a < b);
		}

		public static bool LessThan(decimal a, decimal b)
		{
			return (a < b);
		}

		public static bool LessThan(float a, float b)
		{
			return (a < b);
		}

		public static bool LessThan(double a, double b)
		{
			return (a < b);
		}

		#endregion

		#region GreaterThanOrEquals

		public static bool GreaterThanOrEquals(int a, int b)
		{
			return (a >= b);
		}

		public static bool GreaterThanOrEquals(long a, long b)
		{
			return (a >= b);
		}

		public static bool GreaterThanOrEquals(decimal a, decimal b)
		{
			return (a >= b);
		}

		public static bool GreaterThanOrEquals(float a, float b)
		{
			return (a >= b);
		}

		public static bool GreaterThanOrEquals(double a, double b)
		{
			return (a >= b);
		}

		#endregion

		#region LessThanOrEquals

		public static bool LessThanOrEquals(int a, int b)
		{
			return (a <= b);
		}

		public static bool LessThanOrEquals(long a, long b)
		{
			return (a <= b);
		}

		public static bool LessThanOrEquals(decimal a, decimal b)
		{
			return (a <= b);
		}

		public static bool LessThanOrEquals(float a, float b)
		{
			return (a <= b);
		}

		public static bool LessThanOrEquals(double a, double b)
		{
			return (a <= b);
		}

		#endregion

		public static double Mean(System.Collections.IEnumerable list)
		{
			double total = 0.0;
			int count = 0;
			foreach(object item in list)
			{
				double d = System.Convert.ToDouble(item);
				total += d;
				count++;
			}
			double mean = (total / count);
			return mean;
		}

		public static double StandardDeviation(System.Collections.IEnumerable list)
		{
			return System.Math.Sqrt(Variance(list));
		}

		public static double Variance(System.Collections.IEnumerable list)
		{
			double mean = Mean(list);

			double sum = 0.0;
			int count = 0;
			foreach(object item in list)
			{
				double d = System.Convert.ToDouble(item);
				double deviation = d - mean;
				double deviation_squared = deviation * deviation;
				sum += deviation_squared;
				count++;
			}
			double average_deviation = (sum / count);

			return average_deviation;
		}

		private InstructionSet()
		{
		}

	}
}
